# @FileName  :  auth.py
# @Time      :  2025/8/26 15:19
# @Author    :  Ayanami
# @info      :
from pydantic import BaseModel, EmailStr, validator
from typing import Optional
from enum import Enum


class UserType(str, Enum):
    ADMIN = "admin"
    TEACHER = "teacher"
    STUDENT = "student"


class UserRegister(BaseModel):
    username: str
    email: EmailStr
    phone: str
    password: str
    confirm_password: str
    user_type: UserType

    @validator('confirm_password')
    def passwords_match(cls, v, values, **kwargs):
        if 'password' in values and v != values['password']:
            raise ValueError('passwords do not match')
        return v

    @validator('password')
    def validate_password(cls, v):
        if len(v) < 6:
            raise ValueError('password must be at least 6 characters')
        return v


class UserLogin(BaseModel):
    username: str  # 可以是用户名、邮箱或手机号
    password: str


class Token(BaseModel):
    access_token: str
    refresh_token: str
    token_type: str = "bearer"
    expires_in: int
    user_info: dict


class TokenRefresh(BaseModel):
    refresh_token: str


class UserInfo(BaseModel):
    id: int
    username: str
    email: str
    phone: str
    user_type: UserType
    is_active: bool
    is_verified: bool
    created_at: str

    # 关联信息
    teacher_info: Optional[dict] = None
    student_info: Optional[dict] = None
    agent_info: Optional[dict] = None


class PasswordChange(BaseModel):
    old_password: str
    new_password: str
    confirm_password: str

    @validator('confirm_password')
    def passwords_match(cls, v, values, **kwargs):
        if 'new_password' in values and v != values['new_password']:
            raise ValueError('passwords do not match')
        return v


class PasswordReset(BaseModel):
    email: EmailStr


class PasswordResetConfirm(BaseModel):
    token: str
    new_password: str
    confirm_password: str

    @validator('confirm_password')
    def passwords_match(cls, v, values, **kwargs):
        if 'new_password' in values and v != values['new_password']:
            raise ValueError('passwords do not match')
        return v